﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhaJinHua.Data;
using ZhaJinHua.Web.Models;

namespace ZhaJinHua.Mvc.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return RedirectToAction("Login");
        }

        public ActionResult Login()
        {
            var model = new UserLoginModel();
            WorkContext wc=new WorkContext(HttpContext);
            if (wc.CurrentUser != null)
            {
                model.RealName = wc.CurrentUser.RealName;
            }
            
            return View(model);
        }
        [HttpPost]
        public ActionResult Login(UserLoginModel model)
        {
            if (ModelState.IsValid)
            {
                var userInfo = UserService.Instance.Login(model.RealName);
                var hasJoined = GroupService.Instance.JoinGroup(model.GroupId, userInfo);
                if (hasJoined)
                {
                    WorkContext wc = new WorkContext(HttpContext);
                    wc.CurrentUser = userInfo;
                    return RedirectToAction("Index", "Game", new {Id = model.GroupId});
                }
            }
            return View(model);
        }
    }
}